import React, { useState, useEffect } from 'react';
import { connect } from 'dva';
import EditTemplate from '@/components/EditTemplate';
import { timeFormat } from '@/utils/common';
const BuildEdit = ({ location, dispatch, dict, url }: any) => {
  const [detail, setDetail] = useState({ buildType: 'BUILD' });
  const [onlyRead, setonlyRead] = useState(false);
  const { id, transType, fromType = 3, readOnly } = location.query;
  const transUrl = {
    1: '/api/project/report/v1/annualToStore',
    3: '/api/project/report/v1/planToStore',
  };
  const updateUrl = '/api/project/report/v1/storeReport/update';
  const createUrl = '/api/project/report/v1/storeReport/create';
  const saveUrl = transType ? transUrl[fromType] : id ? updateUrl : createUrl;
  useEffect(() => {
    if (id) {
      const urlObj: any = {
        1: '/api/project/report/v1/annualReport/detail',
        3: '/api/project/report/v1/planReport/detail',
        5: '/api/project/report/v1/storeReport/detail',
      };
      setonlyRead(readOnly);
      dispatch({
        type: '$request/post',
        payload: {
          url: url || urlObj[fromType],
          data: {
            uuid: id,
            page: 0,
            size: 10,
          },
        },
      }).then(res => {
        console.log('储备项目申报详情：', res);

        setDetail({
          ...res,
          professionDataUuid: res.professionData.uuid,
          serviceTypeDataUuid: res.serviceTypeData.uuid,
          projectUuid: res.project.uuid,
          createTime: timeFormat(res.createTime),
          buildType: 'BUILD',
        });
      });
    }
  }, []);
  const filters = [
    {
      title: '项目信息',
      list: [
        {
          key: 'name',
          type: 'input',
          label: '项目名称',
          span: 12,
          placeholder: '请输入项目名称',
        },
        {
          key: ['project', 'no'],
          type: 'text',
          label: '项目ID',
          span: 12,
        },
        {
          key: 'ownerOrgUnit',
          type: 'text',
          label: '业主单位',
          span: 12,
        },
        {
          key: 'professionDataUuid',
          type: 'select',
          label: '行业领域',
          asyncopt: {
            url: '/api/data/v1/getDataList',
            data: {
              code: 'profession',
            },
          },
          span: 12,
          placeholder: '请选择',
          rules: [{ required: true }],
        },
        {
          key: 'buildType', //TODO: 这里未补充
          type: 'select',
          label: '项目性质',
          span: 12,
          disabled: true,
          options: [
            {
              label: '建设',
              value: 'BUILD',
            },
            {
              label: '保障',
              value: 'PURCHASE_SERVICE',
            },
          ],
          // disabled: true,
          placeholder: '请选择',
          rules: [{ required: true }],
        },
        {
          key: 'serviceTypeDataUuid',
          type: 'select',
          label: '项目类型',
          span: 12,
          asyncopt: {
            url: '/api/data/v1/getDataList',
            data: {
              code: 'service',
            },
          },
          placeholder: '请选择',
          rules: [{ required: true }],
        },
        {
          key: 'description',
          type: 'textarea',
          label: ' 项目简介',
          span: 24,
          rules: [{ required: true }],
        },
        {
          key: 'totalInvestment',
          type: 'input',
          label: '总投资',
          span: 24,
          placeholder: '请选择',
          prop: {
            addonAfter: '万元',
          },
          rules: [
            {
              required: true,
              pattern: new RegExp(/^-?\d+/, 'g'),
              message: '只能输入数字',
            },
          ],
        },
        {
          key: 'expectStartYear',
          type: 'select',
          label: ' 拟开建年份',
          span: 12,
          options: dict.fiveYears,
          placeholder: '请选择',
          rules: [{ required: true }],
        },
        {
          key: 'expectCompleteYear',
          type: 'select',
          label: ' 拟建成年份',
          span: 12,
          options: dict.fiveYears,
          placeholder: '请选择',
          rules: [{ required: true }],
        },
        {
          key: 'contactPersonName',
          type: 'input',
          label: ' 联系人',
          span: 12,
          rules: [{ required: true }],
        },
        {
          key: 'contactPersonPhone',
          type: 'input',
          label: ' 联系人电话',
          span: 12,
          rules: [{ required: true }],
        },
        {
          key: 'remark',
          type: 'textarea',
          label: ' 备注',
          span: 24,
        },
        {
          key: 'createTime',
          type: 'text',
          label: '申报日期',
          span: 24,
          hidden: !readOnly,
        },
      ],
    },
  ];
  return (
    <div>
      <EditTemplate
        filters={filters}
        initialValues={detail}
        updateUrl={saveUrl}
        readOnly={onlyRead}
      />
    </div>
  );
};

export default connect((state: any) => state.base)(BuildEdit);
